/**
 * 
 */
package gui.view;

import engine.Service;
import gui.controller.SimulationPanelListener;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.table.DefaultTableModel;

/**
 * @author Elisa Costante
 * 
 */
public class SimulationPanel extends JPanel {

	private static final long serialVersionUID = -8016216775302260682L;
	private SimulationPanelListener listener;
	private JPanel downPanel;
	private JLabel simulationPaneLabel;
	private JLabel callerServiceLabel;
	private JComboBox callerComboBox;
	private JLabel calleeServicesLabel;
	private JComboBox calleeComboBox;
	private JScrollPane allCalleeScroll;
	private JButton addCalleeButton;
	private JButton runButton;
	private JButton removeCalleeButton;
	private JTable allCalleTable;
	private EngineMain frame;
	private DefaultTableModel allCalleTableModel;

	public SimulationPanel(EngineMain frame) {
		super();
		this.frame = frame;
		initPanel();

	}

	private void initPanel() {
		this.setBackground(CustomizedColor.BACK_GROUND_COLOR);
		listener = new SimulationPanelListener(this);

		setDownComponents();
		setDownPanel();
		setMainPanelLayout();

	}

	private void setDownComponents() {

		simulationPaneLabel = new JLabel("Simulation Panel");

		callerServiceLabel = new JLabel("Caller");
		callerComboBox = new JComboBox();
		callerComboBox.setActionCommand("selectedCaller");
		callerComboBox.addActionListener(listener);

		calleeServicesLabel = new JLabel("Candidates Services");
		calleeComboBox = new JComboBox();

		// add the services to both combo
		addServicesToCombo();

		allCalleeScroll = new JScrollPane(getAllCalleTable());
		getAllCalleTable().setFillsViewportHeight(true);

		addCalleeButton = new JButton("Add");
		addCalleeButton.setActionCommand("selectedCallee");
		addCalleeButton.addActionListener(listener);

		removeCalleeButton = new JButton("Remove");
		removeCalleeButton.setActionCommand("removeCallee");
		removeCalleeButton.addActionListener(listener);

		runButton = new JButton("Run Simulation");
		runButton.setActionCommand("runSimulation");
		runButton.addActionListener(listener);

	}

	private void setDownPanel() {
		downPanel = new JPanel();
		downPanel.setBackground(Color.white);
		setDownPanelLayout();

	}

	private void setDownPanelLayout() {
		GroupLayout layout = new GroupLayout(downPanel);
		downPanel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		ParallelGroup horizontal = layout.createParallelGroup(Alignment.CENTER);

		// First Line
		horizontal.addComponent(simulationPaneLabel, 100, 100, Short.MAX_VALUE);

		// Second Line
		SequentialGroup secondLine = layout.createSequentialGroup();
		secondLine.addComponent(callerServiceLabel, 100, 100, Short.MAX_VALUE);
		secondLine.addComponent(callerComboBox, 100, 100, Short.MAX_VALUE);
		horizontal.addGroup(secondLine);

		// Third Line

		SequentialGroup thirdLine = layout.createSequentialGroup();
		thirdLine.addComponent(calleeServicesLabel, 100, 100, Short.MAX_VALUE);

		ParallelGroup calleOrizontalGroup = layout.createParallelGroup();
		calleOrizontalGroup.addComponent(calleeComboBox, 100, 100,
				Short.MAX_VALUE);
		calleOrizontalGroup.addComponent(allCalleeScroll, 100, 100,
				Short.MAX_VALUE);
		thirdLine.addGroup(calleOrizontalGroup);

		ParallelGroup buttonsHOrizontal = layout.createParallelGroup();
		buttonsHOrizontal.addComponent(addCalleeButton, 100, 100, 100);
		buttonsHOrizontal.addComponent(removeCalleeButton, 100, 100, 100);
		thirdLine.addGroup(buttonsHOrizontal);
		horizontal.addGroup(thirdLine);

		// Fourth Line
		horizontal.addComponent(runButton, 150, 150, 150);

		// Vertical

		SequentialGroup vertical = layout.createSequentialGroup();

		// First Line
		vertical.addComponent(simulationPaneLabel, 30, 30, 30);

		// Second Line
		ParallelGroup secondVerticalLine = layout.createParallelGroup();
		secondVerticalLine.addComponent(callerServiceLabel, 30, 30, 30);
		secondVerticalLine.addComponent(callerComboBox, 30, 30, 30);
		vertical.addGroup(secondVerticalLine);

		// Third Line

		ParallelGroup thirdVerticalLine = layout.createParallelGroup();
		thirdVerticalLine.addComponent(calleeServicesLabel, 30, 30, 30);

		SequentialGroup calleVerticalGroup = layout.createSequentialGroup();
		calleVerticalGroup.addComponent(calleeComboBox, 30, 30, 30);
		calleVerticalGroup.addComponent(allCalleeScroll, 50, 50,
				Short.MAX_VALUE);
		thirdVerticalLine.addGroup(calleVerticalGroup);

		SequentialGroup buttonsVertical = layout.createSequentialGroup();
		buttonsVertical.addComponent(addCalleeButton, 30, 30, 30);
		buttonsVertical.addComponent(removeCalleeButton, 30, 30, 30);
		thirdVerticalLine.addGroup(buttonsVertical);
		vertical.addGroup(thirdVerticalLine);

		// Fourth Line
		vertical.addComponent(runButton, 30, 30, 30);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);

	}

	/**
	 * 
	 */
	private void setMainPanelLayout() {
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		ParallelGroup horizontal = layout.createParallelGroup();

		horizontal.addComponent(downPanel, this.getWidth(), this.getWidth(),
				Short.MAX_VALUE);

		SequentialGroup vertical = layout.createSequentialGroup();

		vertical.addComponent(downPanel, this.getHeight(), this.getHeight(),
				Short.MAX_VALUE);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);

	}

	/**
	 * @return
	 */
	public JTable getAllCalleTable() {
		if (allCalleTable == null) {
			allCalleTableModel = new DefaultTableModel();
			allCalleTableModel
					.setColumnIdentifiers(new String[] { "Service Name" });
			allCalleTable = new JTable(allCalleTableModel);
		}
		return allCalleTable;
	}

	/**
	 * @return the frame
	 */
	public EngineMain getFrame() {
		return this.frame;
	}

	/**
	 * Add the services defined in to the Services PAnel to the combo
	 */
	public void addServicesToCombo() {

		ArrayList<Service> monitoredServices = frame.getMonitoredServices();
		if (monitoredServices != null) {
			calleeComboBox.removeAllItems();
			callerComboBox.removeAllItems();

			for (Service service : monitoredServices) {
				calleeComboBox.addItem(service.getIdentifierName());
				callerComboBox.addItem(service.getIdentifierName());
			}
		}
	}

	/**
	 * @return the callerComboBox
	 */
	public JComboBox getCallerComboBox() {
		return this.callerComboBox;
	}

	/**
	 * @return the calleeComboBox
	 */
	public JComboBox getCalleeComboBox() {
		return this.calleeComboBox;
	}

	/**
	 * @return the allCalleTableModel
	 */
	public DefaultTableModel getAllCalleTableModel() {
		return this.allCalleTableModel;
	}

}
